package dao

import javax.inject.Inject
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile
import models.Tables._

import scala.concurrent.{ExecutionContext, Future}

class GrnaDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)
                       (implicit exec: ExecutionContext) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def insert(row:GrnaRow) : Future[Unit] = {
    db.run(Grna   += row).map(_=>())
  }

  def updateState(file:String,state:Int) : Future[Unit] = {
    db.run(Grna.filter(_.file === file).map(_.state).update(state)).map(_=>())
  }

  def getAll: Future[Seq[GrnaRow]] = {
    db.run(Grna.result)
  }

  def deleteByIds(files: Seq[String]): Future[Unit] = {
    db.run(Grna.filter(_.file.inSetBind(files)).delete).map(_ => ())
  }

  def getByFile(file:String) : Future[GrnaRow] = {
    db.run(Grna.filter(_.file === file).result.head)
  }
}
